<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<script type="text/javascript">
	Ext.onReady(function() {

		var cityStore = new Ext.data.JsonStore({
			totalProperty: 'total', 
			root: 'results', 
			sortInfo: {field: 'cityName', direction: 'ASC'},
			remoteSort: true,
			proxy : new Ext.data.HttpProxy({
				url: '<?=site_url()?>admin/get_city',
				method: 'GET'
			}),
			fields: [
				{name: 'cityId'},
				{name: 'cityName'},
				{name: 'cityUrlName'},
				{name: 'cityCouId'},
				{name: 'couName'},
				{name: 'conName'},
				{name: 'cityPriority'},
				{name: 'couUrlName'},
				{name: 'conUrlName'},
				{name: 'cityImageName'},
				{name: 'cityImageNameThumb'},
				{name: 'cityLatitude'},
				{name: 'cityLongitude'},
				{name: 'cityImageAlt'},
				{name: 'e2cTempFlag'},
				{name: 'cityPresenter'},
				{name: 'cityData'},
				{name: 'cityData2'}
			]
		});

		var conContextMenuCity = new Ext.menu.Menu({
			items: [{
				id: 'majFilterCity',
				iconCls: 'icon-zoom',
				text: ''
			}],
			listeners: {
				itemclick: function(item) {
					switch (item.id) {
						case 'majFilterCity': {
							Ext.getCmp('pagePanel').findById('majGrid').expand();
							Ext.getCmp('majGrid').getStore().load({params: {fields:'["cityName"]', query: Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityName}});
						} break;
					}
				}
			}
		});

		var cityGrid = new Ext.grid.GridPanel({
			id: 'cityGrid',
			store: cityStore,
			viewConfig:{
				emptyText: '<?=$this->db->escape_str($this->lang->line("THERE_IS_CURRENTLY_NO_DATA_TO_DISPLAY"))?>',
				getRowClass: function(record, index) {
					if(record.data.cityData == 'N' && record.data.cityPresenter == 'I') return 'presenter_live_without_data';
					else if(record.data.e2cTempFlag == 'I') return 'temp_data_exists';
					else if(record.data.cityPresenter == 'I') return 'presenter_live';
					else return '';
				}
			},
			columns: [
				{header: '<?=$this->db->escape_str($this->lang->line("COUNTRY"))?>', width: 200, sortable: true, dataIndex: 'couName'},
				{id: 'cityName', header: '<?=$this->db->escape_str($this->lang->line("CITY"))?>', width: 200, sortable: true, dataIndex: 'cityName'},
				{header: '<?=$this->db->escape_str($this->lang->line("NOT_EDITED_CONTENT_BUT_VISIBLE"))?>', align: 'center', width: 200, sortable: false, dataIndex: 'cityData2'}
			],
			stripeRows: true,
			autoExpandColumn: 'cityName',
			autoHeight: true,
			autoWidth: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),

			listeners: {

				rowdblclick: function(grid, rowIndex, event) {
					if(rowIndex != 'new') {
						var record = grid.getStore().getAt(rowIndex);
					} else {
						var record = new Object();
						record.data = new Object();
					}
					DataEdit.showWindow('CITY', ((rowIndex != 'new')?(''):('-')) + record.data.cityId)
				}

				,rowcontextmenu: function(grid, index, event) {
					event.stopEvent();
					if(Ext.getCmp('cityGrid').getSelectionModel().getSelections().length > 0) {
						Ext.getCmp('majFilterCity').setText('<?=$this->db->escape_str($this->lang->line("REFINE_THE_COURSES_WITH_THE_SELECTED_ITEM"))?> - ' + Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityName);
						conContextMenuCity.showAt(event.xy);
					}
				}

			},
			border: false,
			frame: false,

			tbar: {
				items: [
					<? if(Common::isSuperAdmin()) { ?>
					{
						icon: '<?=MEDIA?>icon/application_form_add.png',
						text: '<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>',
						handler: function(){
							DataEdit.showWindow('CITY', -1);
						}
					}, {
							xtype: 'tbseparator'
					},
					<? } ?>
					<? if(false and Common::isSuperAdmin()) { ?>
					{
						icon: '<?=MEDIA?>icon/application_form_add.png',
						text: '<?=$this->db->escape_str($this->lang->line("ADD_NEW_INSTITUTE"))?>',
						handler: function(){
							DataEdit.showWindow('INS', -1);
						}
					}, {
							xtype: 'tbseparator'
					},
					<? } ?>
					{
						icon: '<?=MEDIA?>icon/application_form_edit.png',
						text: '<?=$this->db->escape_str($this->lang->line("EDIT"))?>',
						ref: '../editBtn',
						disabled: true,
						handler: function() {
							Ext.getCmp('cityGrid').fireEvent('rowdblclick', Ext.getCmp('cityGrid'), Ext.getCmp('cityGrid').store.indexOf(Ext.getCmp('cityGrid').getSelectionModel().getSelected()), new Object());
						}
					}, {
							xtype: 'tbseparator'
					},
					{
						icon: '<?=MEDIA?>icon/application_form_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("DELETE"))?>',
						ref: '../removeBtn',
						disabled: true,
						handler: function() {
							DataEdit.delete('CITY', Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityId, Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityName);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_preview.png',
						text: '<?=$this->db->escape_str($this->lang->line("PREVIEW"))?>',
						ref: '../previewBtn',
						disabled: true,
						handler: function(){
							window.open('<?=site_url()?>preview/' + Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.conUrlName + '/' + Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.couUrlName + '/' + Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityUrlName);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish.png',
						text: '<?=$this->db->escape_str($this->lang->line("PUBLICATION"))?>',
						ref: '../publishBtn',
						disabled: true,
						handler: function(){
							DataEdit.publish('CITY', Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityId, Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityName, 1);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_UNPUBLISHED_DATA"))?>',
						ref: '../publishBtnDelete',
						disabled: true,
						handler: function(){
							DataEdit.publish('CITY', Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityId, Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityName, 0);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish_remove.png',
						text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_PUBLISHED_DATA"))?>',
						ref: '../unpublishBtn',
						disabled: true,
						handler: function(){
							DataEdit.unpublish('CITY', Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityId, Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityName, 2);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/image.png',
						text: '<?=$this->db->escape_str($this->lang->line("MANAGING_IMAGES"))?>',
						ref: '../imageBtn',
						disabled: true,
						handler: function(){
							DataEdit.image('CITY', Ext.getCmp('cityGrid').getSelectionModel().getSelections()[0].data.cityId);
						}
					}
				]
			},

			bbar: 
				new Ext.PagingToolbar({
					pageSize: <?=$this->config->item("adminGridPageSize")?>,
					store: cityStore,
					displayInfo: true,
					plugins: new Ext.ux.ProgressBarPager()
				}),

			plugins:[
				new Ext.ux.grid.Search({
					minChars:2,
					iconCls: 'icon-zoom',
					searchText:'<?=$this->db->escape_str($this->lang->line("SEARCH"))?>',
					position:'top',
					minCharsTipText:'<?=$this->db->escape_str($this->lang->line("MINIMUM_TWO_CHARACTERS_IS_REQUIRED"))?>',
					selectAllText: '<?=$this->db->escape_str($this->lang->line("SELECT_ALL"))?>',
					width:150,
					disableIndexes:['cityData2'],
					autoFocus:true
				})]

		});

		Ext.getCmp('cityGrid').render('grid-table-city');

		Ext.getCmp('cityGrid').getSelectionModel().on('selectionchange', function(sm){
			Ext.getCmp('cityGrid').removeBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('cityGrid').editBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('cityGrid').publishBtn.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('cityGrid').publishBtnDelete.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('cityGrid').unpublishBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('cityGrid').previewBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('cityGrid').imageBtn.setDisabled(sm.getCount() < 1);
		});

		cityStore.load({params:{start:0, limit:<?=$this->config->item("adminGridPageSize")?>}});


	})
</script>


<div id="grid-table-city"></div>